package com.jorigin.dcball.util;

import com.jorigin.dcball.po.DoubleBall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 数字生成工具类
 * 33选6全排列生成工具类
 * @author lzhq
 */
public class DcballFactoryUtils {

	private static final Logger logger = LoggerFactory.getLogger(DcballFactoryUtils.class);
	//最大数
	private static int LARGEST_RED = 33;
	private static int LARGEST_BLUE = 16;


	static List<Integer>  temp = new ArrayList<Integer>();
	static List<List<Integer>> ans = new ArrayList<List<Integer>>();

	public static List<List<Integer>> combine(int n, int k) {
		dfs(1, n, k);
		return ans;
	}

	public static void dfs(int cur, int n, int k) {
		// 剪枝：temp 长度加上区间 [cur, n] 的长度小于 k，不可能构造出长度为 k 的 temp
		if (temp.size() + (n - cur + 1) < k) {
			return;
		}
		// 记录合法的答案
		if (temp.size() == k) {
			ans.add(new ArrayList<Integer>(temp));
			return;
		}
		// 考虑选择当前位置
		temp.add(cur);
		dfs(cur + 1, n, k);
		temp.remove(temp.size() - 1);
		// 考虑不选择当前位置
		dfs(cur + 1, n, k);
	}


	/**
	 * 1-33
	 * 排列组合6个数字
	 *
	 * C(33,6) = 1107568
	 * @return  2389
	 */
	public static void createSix(List dbopenlist){
		int count = 1;
		int haveCount = 0;
		DoubleBall db = new DoubleBall();
		for (int i=1 ;i<=LARGEST_RED-5 ;i++){
			db.setRed1(i);
			for (int ii = i+1;ii<=LARGEST_RED-4;ii++){
				db.setRed2(ii);
				for (int iii = ii+1;iii<=LARGEST_RED -3;iii++){
					db.setRed3(iii);
					for (int iiii = iii+1;iiii<=LARGEST_RED -2;iiii++){
						db.setRed4(iiii);
						for (int iiiii = iiii+1;iiiii<=LARGEST_RED -1 ;iiiii++){
							db.setRed5(iiiii);
							for (int iiiiii = iiiii+1;iiiiii<=LARGEST_RED ;iiiiii++){
								db.setRed6(iiiiii);
								//排除已开
								if(!dbopenlist.contains(db)){
									if(dcbFilter(db)){
										logger.info(count++ +":"+ db.toString());
									}
                                }else {
									haveCount++;
								}
							}
						}
					}
				}
			}
		}
		logger.info("haveing :"+ haveCount);
	}

	/**
	 * 递归生成红球全排列
	 * 33选6
	 */
	public static void buildRed(DoubleBall db ,int ed , int times){
		if(db == null){
			db = new DoubleBall();
		}
		System.out.println(db.toString());
	}
	/**
	 * 挑选
	 * 1、231
	 * 2、第一个小于4
	 * 3、
	 * @return
	 */
	public static boolean dcbFilter(DoubleBall db){
		if(db.getRed2()<=11&&db.getRed3()>11&&db.getRed5()<=22
				&&(db.getRed2()-db.getRed1())>1
				&&(db.getRed2()-db.getRed1())<5
				&&(db.getRed4()-db.getRed3())<(db.getRed5()-db.getRed4())){
			return true ;
		}

		return false ;


	}



	/**
	 * 生成33选6的全排列，过滤不满足条件的数
	 * @param args
	 */
	public static void main(String[] args) {

        /*ArrayList<DoubleBall> dbopenlist = DcballFileUtils.dbopenlist;
        createSix(dbopenlist);*/
//		DcballFactoryUtils.buildRed(new DoubleBall(),1,1);

		DcballFactoryUtils.combine(33,6);
		ans.forEach(list->{
			System.out.println(list);
		});
		System.out.println(ans.size());
		System.out.println("1111111111");
	}
}
